package com.shenhaofeng.demo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shenhaofeng.app.adapter.ModuleAdapter;
import com.shenhaofeng.app.adapter.SingleTypeMoreAdapter;
import com.shenhaofeng.app.component.BaseActivity;

/**
 * Created by haofeng on 2016/9/26.
 */

public abstract class IntentItemActivity extends BaseActivity {


    private RecyclerView mRecyclerView;

    private SingleTypeMoreAdapter<IntentItemActivity.IntentItemViewHolder, IntentItem> mAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new IntentItemActivity.IntentItemAdapter();
        setContentView(R.layout.act_main);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
        setIntentItems(mAdapter.getModule());
    }


    protected abstract void setIntentItems(ModuleAdapter.Module<IntentItem> module);


    private class IntentItemAdapter extends SingleTypeMoreAdapter<IntentItemActivity.IntentItemViewHolder, IntentItem> {

        @Override
        protected void onBindSingleItemViewHolder(IntentItemActivity.IntentItemViewHolder holder, int position) {
            final IntentItem intentItem = getModule().getItemFromAdapterPosition(position);
            holder.titleTv.setText(intentItem.getTitle());
            holder.contentTv.setText(intentItem.getContent());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.getContext().startActivity(new Intent(v.getContext(), intentItem.getCls()));
                }
            });
        }

        @Override
        protected IntentItemActivity.IntentItemViewHolder onCreateSingleViewHolder(ViewGroup parent, int viewType) {
            return new IntentItemActivity.IntentItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.act_main_item, parent, false));
        }
    }

    private class IntentItemViewHolder extends RecyclerView.ViewHolder {

        public TextView titleTv, contentTv;


        public IntentItemViewHolder(View itemView) {
            super(itemView);
            titleTv = (TextView) itemView.findViewById(R.id.title);
            contentTv = (TextView) itemView.findViewById(R.id.content);
        }
    }
}